package ferp.android.widgets;

import android.content.Context;
import android.content.res.Resources;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import ferp.android.GUI;
import ferp.android.R;
import ferp.core.game.Bid;

/**
 * User: igorgok
 * Date: 12/30/13 12:25 PM
 */
public class BidView extends ViewGroup
{
  public BidView(Context context, float fx, float fy) {this(context, null, fx, fy);}
  public BidView(Context context, AttributeSet attrs) {this(context, attrs, 1, 1);}
  public BidView(Context context, AttributeSet attrs, float fx, float fy)
  {
    super(context, attrs);

    // inflate the view
    ((LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.bid_view, this, true);

    this.fx = fx;
    this.fy = fy;

    text = (CustomTextView)findViewById(R.id.bid_view_text);
    suit = (SuitView)findViewById(R.id.bid_view_suit);
  }

  // show bid
  public void show(Bid bid) {show(bid, false);}
  public void show(Bid bid, boolean abbreviated)
  {
    // bid type might be null when confirmation dialog is restored even without being shown
    if (bid.type != null)
      if (bid.isPlay())
      {
        text.set(String.valueOf(bid.tricks), GUI.getBidColor(bid));
        suit.set(bid.trump);

        // show text and image views
        text.setVisibility(VISIBLE);
        suit.setVisibility(VISIBLE);
      }
      else
      {
        Resources r = getResources();
        String s = bid.isCheck10() ? r.getString(R.string.bid_mine) + " " + bid.tricks : r.getString(r.getIdentifier("input_" + bid.toString(), "string", getContext().getPackageName()));

        // show only first letter if abbreviated flag is set (this only happens in pool dialog)
        if (abbreviated)
          s = "" + s.charAt(0);

        // hide image
        suit.setVisibility(GONE);
        text.set(s, GUI.Color.NORMAL);
      }
  }

/*
  @Override
  public void setEnabled(boolean enabled)
  {
    super.setEnabled(enabled);

    text.setTextColor(enabled ? GUI.Color.NORMAL : GUI.Color.DISABLE);

    // image couldn't be changed here as we don't know the bid -
    // the ancestors classes (BidButton, SuitButton) do this
  }
*/

  @Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
  {
    int nw = Math.round(MeasureSpec.getSize(widthMeasureSpec) * fx),
        nwp = nw - getPaddingLeft() - getPaddingRight(),
        nh = Math.round(MeasureSpec.getSize(heightMeasureSpec) * fy),
        nhp = nh > 0 ? nh - getPaddingTop() - getPaddingBottom() : 0;

    // measure the text view, only height matters for now
    text.measure(MeasureSpec.UNSPECIFIED, nhp > 0 ? MeasureSpec.makeMeasureSpec(nhp, MeasureSpec.EXACTLY) : MeasureSpec.UNSPECIFIED);

    if (suit.getVisibility() == VISIBLE)
    {
      // suit is visible
      // max width for one widget
      int mw = nwp / 2,
          sms = nhp > 0 ? MeasureSpec.makeMeasureSpec(text.getMeasuredHeight(), MeasureSpec.EXACTLY) : MeasureSpec.UNSPECIFIED;

      // fit number of tricks by width if needed (it should occupy no more than nw/2)
      text.fitTextByWidth(mw);
      // measure the suit
      suit.measure(sms, sms);

      setMeasuredDimension(text.getMeasuredWidth() + suit.getMeasuredWidth() + getPaddingLeft() + getPaddingRight(), text.getMeasuredHeight() + getPaddingTop() + getPaddingBottom());
    }
    else
    {
      // suit is not visible
      // fit bid text
      text.fitTextByWidth(nwp);

      setMeasuredDimension(text.getMeasuredWidth() + getPaddingLeft() + getPaddingRight(), text.getMeasuredHeight() + getPaddingTop() + getPaddingBottom());
    }

/*
    if (getTag() != null)
    {
      Log.debug(Log.TAG, "BV measure - " + GUI.m2s(widthMeasureSpec, heightMeasureSpec) + " -> " + getMeasuredWidth() + "x" + getMeasuredHeight() + ", tvh: " + text.getMeasuredHeight() + ", ts: " + text.getTextSize());
      Log.debug(Log.TAG, "------------ pt: " + getPaddingTop() + ", pb: " + getPaddingBottom());
    }
*/
  }

  @Override
  protected void onLayout(boolean changed, int l, int t, int r, int b)
  {
    //int w2 = text.getMeasuredWidth() + (suit.getVisibility() == VISIBLE ? suit.getMeasuredWidth() : 0) - getPaddingLeft() - getPaddingRight();
    int w2 = Math.max(getMeasuredWidth() - getPaddingLeft() - getPaddingRight(), text.getMeasuredWidth() + (suit.getVisibility() == VISIBLE ? suit.getMeasuredWidth() : 0));
    int x = GUI.center(w2, 0, r - l), y = GUI.center(text.getMeasuredHeight(), 0, b - t);
    int tr = x + text.getMeasuredWidth();

    text.layout(x, y, tr, y + text.getMeasuredHeight());

    if (suit.getVisibility() == VISIBLE)
    {
      int sy = GUI.center(suit.getMeasuredHeight(), 0, getMeasuredHeight());

      suit.layout(++tr, sy, tr + suit.getMeasuredWidth(), sy + suit.getMeasuredHeight());
    }

/*
    if (getTag() != null)
      Log.debug(Log.TAG, "BV onLayout " + l + "x" + t + " - " + r + "x" + b + ", ts: " + text.getTextSize());
*/
  }

  // factors
  private float fx, fy;
  // elements
  private CustomTextView text;
  private SuitView suit;
}
